<?php

class Core_Library_Validator {
    /**
     *
     * @param type $name
     * @param type $tableName
     * @return function will return false if user exist and true if user doesn't exist
     */
    public function user($name, $tableName) {
        $sql = 'select ' . $name . ' from ' . $tableName;
        $result = mysql_query($sql);
        $num = mysql_num_rows($result);
        if ($num ==0) {
            return "no";
        } else {
            return "yes";
        }
        //var_dump($result); die;
    }
    /**
     *
     * @param type $password
     * @param type $repassword
     * @param type $code
     * @return boolean
     * if code true then return true if md5($password) === $repassword and return false if md5($password) === $repassword  
     */
    public function password($password, $repassword, $code = false) {

        if ($code == false) {
            if ($password === $repassword) {
                return true;
            } else {
                return false;
            }
        } else {
            if (md5($password) === $repassword) {
                return true;
            } else {
                return false;
            }
        }
    }

    public function email($email) {
        if (preg_match("|^([a-z0-9_\.\-]{1,50})@([a-z0-9\.\-]{2,20})\.([a-z]{2,25})|is", strtolower($email))) {
            return true;
        } else {
            return false;
        }
    }

    public function escapeString($array) {
        $varsArray = array();
        if (is_array($array)) {
            foreach ($array as $key => $value) {
                $varsArray[$key] = mysql_escape_string($value);
            }
            return $varsArray;
        }
        return mysql_escape_string($array);
    }

}